package com.haoyeteng.sxbd.modular.common.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.haoyeteng.sxbd.core.mongodb.MongoFileWriter;
import com.haoyeteng.sxbd.modular.common.service.IUploadService;

/**
 * 上传接口实现类
 * @author 理想
 *
 */
@Service
public class UploadServiceImpl implements IUploadService {
	
	@Autowired
	MongoFileWriter mongoFileWriter;

	@Override
	public String addFile(MultipartFile file, String newFileName) throws IOException {
		String fileId = UUID.randomUUID().toString();
		String contentType = file.getContentType();
		if (!mongoFileWriter.toWrite(file.getInputStream(), newFileName, fileId, contentType)) {
			return null;
		}
		return fileId;
	}

	@Override
	public String addFile(InputStream is,String contentType, String newFileName) throws IOException {
		String fileId = UUID.randomUUID().toString();
		if (!mongoFileWriter.toWrite(is, newFileName, fileId, contentType)) {
			return null;
		}
		return fileId;
	}

	@Override
	public Boolean deleteFile(String fileid) {
		try {
			if (null != fileid) {
				mongoFileWriter.toDelete(fileid);
				return true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}


}
